from pyDes import des, CBC, ECB, PAD_PKCS5
import binascii

# 秘钥
# KEY = 'Kk8y6o84zUk='
KEY = 'S2s4eTZv'


def des_encrypt(s):
    """
    DES 加密
    :param s: 原始字符串
    :return: 加密后字符串，16进制
    """
    secret_key = KEY
    iv = secret_key
    k = des(secret_key, CBC, iv, pad=None, padmode=PAD_PKCS5)
    en = k.encrypt(s, padmode=PAD_PKCS5)
    # print(en)
    print(binascii.b2a_base64(en))
    return binascii.b2a_hex(en)


def des_descrypt(s):
    """
    DES 解密
    :param s: 加密后的字符串，16进制
    :return:  解密后的字符串
    """
    secret_key = KEY
    iv = secret_key
    k = des(secret_key, CBC, iv, pad=None, padmode=PAD_PKCS5)
    de = k.decrypt(binascii.a2b_hex(s), padmode=PAD_PKCS5)
    print(binascii.a2b_base64(s))
    return de

def des_encrypt_ecb(s):
    """
    DES 加密
    :param s: 原始字符串
    :return: 加密后字符串，16进制
    """
    #DES d3 = new DES(Mode.ECB, Padding.PKCS5Padding,"S2s4eTZv".getBytes());//"S2s4eTZv".getBytes()
    # System.out.println("ECB:"+d3.encryptHex("root"));//038278684c555c36
    secret_key = KEY
    # iv = secret_key
    k = des(secret_key, ECB, IV=None, pad=None, padmode=PAD_PKCS5)
    en = k.encrypt(s, padmode=PAD_PKCS5)
    # print(en)
    print(binascii.b2a_base64(en))
    return binascii.b2a_hex(en)


s = 'root'
enc = des_encrypt(s)
print(enc)
d = des_descrypt(enc)
print(d)
ecb = des_encrypt_ecb(s)
print("ecb",str(ecb))
